<!--字典类型-->
<script setup lang="ts">
import { getPage, getItemPage } from "@/api/order";

import {
  OrderPageVO,
  OrderQuery,
  OrderItemQuery,
  OrderItemPageVO,
} from "@/api/order/types";

defineOptions({
  name: "DictType",
  inheritAttrs: false,
});

const queryFormRef = ref(ElForm);
// const dataFormRef = ref(ElForm);
const queryItemFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);
const itemTotal = ref(0);

const dateTimeRange = ref("");
const queryParams = reactive<OrderQuery>({
  pageNum: 1,
  pageSize: 10,
});
const queryItemParams = reactive<OrderItemQuery>({
  pageNum: 1,
  pageSize: 5,
});

const orderList = ref<OrderPageVO[]>();
const orderItemList = ref<OrderItemPageVO[]>();

watch(dateTimeRange, (newVal) => {
  if (newVal) {
    queryParams.startTime = newVal[0];
    queryParams.endTime = newVal[1];
  }
});

const dialog = reactive({
  visible: false,
});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getPage(queryParams)
    .then(({ data }) => {
      orderList.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 订单物品查询 */
function handleItemQuery() {
  getItemPage(queryItemParams).then(({ data }) => {
    orderItemList.value = data.list;
    itemTotal.value = data.total;
  });
}

/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  dateTimeRange.value = "";
  queryParams.pageNum = 1;
  queryParams.recvName = undefined;
  queryParams.startTime = undefined;
  queryParams.endTime = undefined;
  handleQuery();
}

function resetItemQuery() {
  queryItemFormRef.value.resetFields();
  queryItemParams.title = undefined;
  handleItemQuery();
}

/**
 * 打开字典类型表单弹窗
 *
 * @param dicTypeId 字典类型ID
 */
function openDialog(orderId?: number) {
  dialog.visible = true;
  queryItemParams.orderId = orderId;
  handleItemQuery();
}

/** 关闭字典类型弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/**  重置字典类型表单 */
function resetForm() {
  // dataFormRef.value.resetFields();
  // dataFormRef.value.clearValidate();
  // formData.id = undefined;
  // formData.status = 1;
  queryItemFormRef.value.resetFields();
  queryItemParams.title = undefined;
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="name">
          <el-input
            v-model="queryParams.recvName"
            placeholder="收件人姓名"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="全部"
            clearable
            class="!w-[100px]"
          >
            <el-option label="待付款" value="0" />
            <el-option label="待收货" value="1" />
            <el-option label="待评价" value="2" />
            <el-option label="退货退款" value="3" />
          </el-select>
        </el-form-item>

        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateTimeRange"
            type="datetimerange"
            range-separator="~"
            start-placeholder="开始时间"
            end-placeholder="截止时间"
            value-format="YYYY-MM-DD hh:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery()"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery()"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        v-loading="loading"
        highlight-current-row
        :data="orderList"
        border
      >
        <el-table-column label="收件人姓名" prop="recvName" width="200" />
        <el-table-column label="收件人电话" prop="recvPhone" width="200" />
        <el-table-column label="订单状态" align="center" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0" type="warning">待付款</el-tag>
            <el-tag v-else-if="scope.row.status === 1" type="success">
              待收货
            </el-tag>
            <el-tag v-else-if="scope.row.status === 2" type="primary">
              待评价
            </el-tag>
            <el-tag v-else-if="scope.row.status === 3" type="info">
              退货退款
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="订单总价" prop="totalPrice" align="center" />
        <el-table-column label="收货省" prop="recvProvince" align="center" />
        <el-table-column label="收货市" prop="recvCity" align="center" />
        <el-table-column label="收货区" prop="recvArea" align="center" />
        <el-table-column label="详细地址" prop="recvAddress" align="center" />
        <el-table-column
          label="订单创建时间"
          prop="createdTime"
          align="center"
        />
        <el-table-column label="订单支付时间">
          <template #default="scope">
            <span>{{ scope.row.payTime ? scope.row.payTime : "待支付" }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="220">
          <template #default="scope">
            <el-button
              type="primary"
              link
              size="small"
              @click.stop="openDialog(scope.row.id)"
              ><i-ep-Collection />订单详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!--订单项目弹窗-->
    <el-dialog
      v-model="dialog.visible"
      title="订单物品列表"
      @close="closeDialog"
    >
      <div class="search-container">
        <el-form ref="queryItemFormRef" :model="queryItemParams" :inline="true">
          <el-form-item label="关键字" prop="name">
            <el-input
              v-model="queryItemParams.title"
              placeholder="商品名称"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleItemQuery()"
              ><i-ep-search />搜索</el-button
            >
            <el-button @click="resetItemQuery()"
              ><i-ep-refresh />重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <el-card shadow="never" class="table-container">
        <el-table
          v-loading="loading"
          highlight-current-row
          :data="orderItemList"
          border
        >
          <el-table-column label="图片" prop="image" align="center">
            <el-image :src="image" />
          </el-table-column>
          <el-table-column label="商品标题" prop="title" width="200" />
          <el-table-column label="商品单价" prop="price" align="center" />
          <el-table-column label="商品数量" prop="num" width="200" />
        </el-table>

        <pagination
          v-if="itemTotal > 0"
          v-model:total="itemTotal"
          v-model:page="queryItemParams.pageNum"
          v-model:limit="queryItemParams.pageSize"
          :page-sizes="[5, 10, 20, 30, 50]"
          @pagination="handleItemQuery"
        />
      </el-card>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
